package org.shj.springcloud.movie.controller;


import org.shj.springcloud.movie.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;

@RestController
public class MovieController {
	
	private String userMicroServiceName = "microservice-provider-user";
	
	@Autowired
	private RestTemplate restTemplate;
	
	@HystrixCommand(fallbackMethod="findByIdFallback", commandProperties = {
			@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "5000"),
			@HystrixProperty(name = "metrics.rollingStats.timeInMilliseconds", value = "10000")
	}, threadPoolProperties = {
			@HystrixProperty(name = "coreSize", value = "1"),
			@HystrixProperty(name = "maxQueueSize", value = "10")
	})
	@GetMapping("/user/{uid}")
	public User findById(@PathVariable String uid){
		return this.restTemplate.getForObject("http://" + userMicroServiceName + "/" + uid, User.class);
	}
	
	public User findByIdFallback(String uid){
		User user = new User();
		user.setId("Default ID");
		user.setUsername("Default User");
		user.setEmail("Default Email");
		return user;
	}
	
}
